package com.emser.unc.client;


import com.emser.unc.client.event.AddCustomerEvent;
import com.emser.unc.client.event.AddCustomerEventHandler;
import com.emser.unc.client.event.AddProductEvent;
import com.emser.unc.client.event.AddProductEventHandler;
import com.emser.unc.client.event.AddSellerEvent;
import com.emser.unc.client.event.AddSellerEventHandler;
import com.emser.unc.client.event.AdminNoSaleReportEvent;
import com.emser.unc.client.event.AdminNoSaleReportEventHandler;
import com.emser.unc.client.event.AdminSaleReportEvent;
import com.emser.unc.client.event.AdminSaleReportEventHandler;
import com.emser.unc.client.event.EditCustomerEvent;
import com.emser.unc.client.event.EditCustomerEventHandler;
import com.emser.unc.client.event.EditCustomerListEvent;
import com.emser.unc.client.event.EditCustomerListEventHandler;
import com.emser.unc.client.event.EditProductEvent;
import com.emser.unc.client.event.EditProductEventHandler;
import com.emser.unc.client.event.EditProductListEvent;
import com.emser.unc.client.event.EditProductListEventHandler;
import com.emser.unc.client.event.EditSellerEvent;
import com.emser.unc.client.event.EditSellerEventHandler;
import com.emser.unc.client.event.GoBackEvent;
import com.emser.unc.client.event.GoBackEventHandler;
import com.emser.unc.client.event.LogoutEvent;
import com.emser.unc.client.event.LogoutEventHandler;
import com.emser.unc.client.event.MenuEvent;
import com.emser.unc.client.event.MenuEventHandler;
import com.emser.unc.client.event.SaleReportDetailEventHandler;
import com.emser.unc.client.event.SaleReportEventDetail;
import com.emser.unc.client.event.ShowMapEvent;
import com.emser.unc.client.event.ShowMapEventHandler;
import com.emser.unc.client.presenter.CustomerPopupPresenter;
import com.emser.unc.client.presenter.CustomersPresenter;
import com.emser.unc.client.presenter.DevicesPresenter;
import com.emser.unc.client.presenter.HeaderPresenter;
import com.emser.unc.client.presenter.MapGeolocationPresenter;
import com.emser.unc.client.presenter.MenuPresenter;
import com.emser.unc.client.presenter.NoSaleReportPresenter;
import com.emser.unc.client.presenter.Presenter;
import com.emser.unc.client.presenter.ProductPopupPresenter;
import com.emser.unc.client.presenter.ProductPresenter;
import com.emser.unc.client.presenter.ReportsPresenter;
import com.emser.unc.client.presenter.SaleReportDetailPresenter;
import com.emser.unc.client.presenter.SaleReportPresenter;
import com.emser.unc.client.presenter.SellerCustomersPresenter;
import com.emser.unc.client.presenter.SellerPopupPresenter;
import com.emser.unc.client.presenter.SellerPresenter;
import com.emser.unc.client.presenter.SellerProductsPresenter;
import com.emser.unc.client.view.CustomerPopupView;
import com.emser.unc.client.view.CustomersView;
import com.emser.unc.client.view.DevicesView;
import com.emser.unc.client.view.HeaderView;
import com.emser.unc.client.view.MapGeolocationView;
import com.emser.unc.client.view.MenuView;
import com.emser.unc.client.view.NoSaleReportView;
import com.emser.unc.client.view.ProductPopupView;
import com.emser.unc.client.view.ProductsView;
import com.emser.unc.client.view.ReportsView;
import com.emser.unc.client.view.SaleReportDetailView;
import com.emser.unc.client.view.SaleReportsView;
import com.emser.unc.client.view.SellerCustomerListView;
import com.emser.unc.client.view.SellerPopupView;
import com.emser.unc.client.view.SellerProductsListView;
import com.emser.unc.client.view.SellerView;
import com.emser.unc.client.view.WelcomeView;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
public class AppController implements Presenter, ValueChangeHandler<String> {
	private final SimpleEventBus eventBus; 
	private HasWidgets centerPanel, northPanel, westPanel;;
	

	public void setContainers(HasWidgets panelNorth,HasWidgets panelWest,HasWidgets centerPanel) {
		this.northPanel = panelNorth;
		this.westPanel = panelWest;
		this.centerPanel = centerPanel;
	}

	public AppController( SimpleEventBus eventBus) {
	    this.eventBus = eventBus;
	    bind();
	}	
	
	private void bind() {
	    History.addValueChangeHandler(this);  
	    eventBus.addHandler(LogoutEvent.TYPE, new LogoutEventHandler() {
            @Override
            public void onLogout(LogoutEvent event) {
					doLogout();
            }
        });
	    eventBus.addHandler(AddProductEvent.TYPE, new AddProductEventHandler(){
			@Override
			public void onAdd(AddProductEvent event) {
				ProductPopupPresenter p = new ProductPopupPresenter(eventBus,new ProductPopupView(),event.getRPCserviceSeller());
				p.configPopup(CloudMobile.get().getCurrentUser(), CloudMobile.constants.titleAddProduct());
			}
	    });
	    eventBus.addHandler(EditProductEvent.TYPE, new EditProductEventHandler(){
			@Override
			public void onEdit(EditProductEvent event) {
				ProductPopupPresenter p = new ProductPopupPresenter(eventBus,new ProductPopupView(),event.getRPCserviceProduct());
				p.configPopup(CloudMobile.get().getCurrentUser(), CloudMobile.constants.titleEditProduct(),event.getProduct());
			}
	    });
	    eventBus.addHandler(AdminSaleReportEvent.TYPE, new AdminSaleReportEventHandler(){
			@Override
			public void onChange(AdminSaleReportEvent event) {
				SaleReportPresenter p = new SaleReportPresenter(CloudMobile.get().getCurrentUser(),eventBus, new SaleReportsView());
				p.go(centerPanel);
			}
	    });
	    eventBus.addHandler(AdminNoSaleReportEvent.TYPE, new AdminNoSaleReportEventHandler(){
			@Override
			public void onChange(AdminNoSaleReportEvent event) {
				NoSaleReportPresenter p = new NoSaleReportPresenter(CloudMobile.get().getCurrentUser(),eventBus, new NoSaleReportView());
				p.go(centerPanel);
			}	
	    });
	    eventBus.addHandler(AddSellerEvent.TYPE, new AddSellerEventHandler(){
			@Override
			public void onAdd(AddSellerEvent event) {
				SellerPopupPresenter p = new SellerPopupPresenter(eventBus,event.getRPCserviceSeller(),new SellerPopupView());
				p.configPopup(CloudMobile.get().getCurrentUser(), CloudMobile.constants.titleAddSeller());
			}	
	    });
	    eventBus.addHandler(AddCustomerEvent.TYPE, new AddCustomerEventHandler(){
			@Override
			public void onAdd(AddCustomerEvent event) {
				CustomerPopupPresenter p = new CustomerPopupPresenter(eventBus,event.getRPCserviceCustomer(), new CustomerPopupView());
				p.configPopup(CloudMobile.get().getCurrentUser(), CloudMobile.constants.titleAddCustomer());
			}	
	    });
	    eventBus.addHandler(EditCustomerEvent.TYPE, new EditCustomerEventHandler(){
			@Override
			public void onEdit(EditCustomerEvent event) {
				CustomerPopupPresenter p = new CustomerPopupPresenter(eventBus,event.getRPCserviceCustomer(),new CustomerPopupView());
				p.configPopup(CloudMobile.get().getCurrentUser(), CloudMobile.constants.titleEditCustomer(), event.getCustomer());
			}	
	    });
	    eventBus.addHandler(EditSellerEvent.TYPE, new EditSellerEventHandler(){
			@Override
			public void onEdit(EditSellerEvent event) {
				SellerPopupPresenter p = new SellerPopupPresenter(eventBus,event.getRPCserviceSeller(),new SellerPopupView());
				p.configPopup(CloudMobile.get().getCurrentUser(),CloudMobile.constants.titleEditSeller(),event.getSeller());
			}	
	    });
	    eventBus.addHandler(EditProductListEvent.TYPE, new EditProductListEventHandler(){
			@Override
			public void onEdit(EditProductListEvent event) {
				SellerProductsPresenter p = new SellerProductsPresenter(CloudMobile.get().getCurrentUser(),event,eventBus,new SellerProductsListView());
				p.go(centerPanel);
			}	
	    });
	    eventBus.addHandler(EditCustomerListEvent.TYPE, new EditCustomerListEventHandler(){
			@Override
			public void onEdit(EditCustomerListEvent event) {
				SellerCustomersPresenter p = new SellerCustomersPresenter(CloudMobile.get().getCurrentUser(),event,eventBus,new SellerCustomerListView());
				p.go(centerPanel);
			}	
	    });
	    eventBus.addHandler(GoBackEvent.TYPE, new GoBackEventHandler(){
			@Override
			public void onGoBack(GoBackEvent event) {
				SellerPresenter p = event.getSp();
				p.go(centerPanel);
			}	
	    });
	    eventBus.addHandler(MenuEvent.TYPE, new MenuEventHandler(){
			@Override
			public void onChange(MenuEvent event) {
				doChangeMenu(event.getType());
			}	
	    });
	    eventBus.addHandler(SaleReportEventDetail.TYPE, new SaleReportDetailEventHandler(){
			@Override
			public void onChange(SaleReportEventDetail event) {
				SaleReportDetailPresenter p = new SaleReportDetailPresenter(CloudMobile.get().getCurrentUser(),eventBus,new SaleReportDetailView());
				p.setIdCustomer(event.getCustomerID());
				p.setIdReport(event.getIdReport());
				p.setUserNSeller(event.getUserNSeller());
				p.go(centerPanel);
			}	
	    });
	    eventBus.addHandler(ShowMapEvent.TYPE, new ShowMapEventHandler(){
			@Override
			public void onChange(ShowMapEvent event) {
				MapGeolocationPresenter p = new MapGeolocationPresenter(new MapGeolocationView(),event.getSerialDevice(),CloudMobile.get().getCurrentUser());
				p.go(centerPanel);
			}	
	    });

	 }
	
	private void doChangeMenu(int type){
		switch(type){
		case 1: SellerPresenter sp = new SellerPresenter( CloudMobile.get().getCurrentUser(),eventBus, new SellerView());
    			sp.go(centerPanel);
    			break;
		case 2: CustomersPresenter cp = new CustomersPresenter(CloudMobile.get().getCurrentUser(),eventBus, new CustomersView());
				cp.go(centerPanel);
				break;
		case 3: DevicesPresenter dp = new DevicesPresenter(CloudMobile.get().getCurrentUser(),eventBus,new DevicesView());
				dp.go(centerPanel);
				break;
		case 4: ProductPresenter pp = new ProductPresenter(CloudMobile.get().getCurrentUser(),eventBus,new ProductsView());
				pp.go(centerPanel);
				break;
		case 5:	ReportsPresenter rp = new ReportsPresenter(eventBus,new ReportsView());
				rp.go(centerPanel);
				break;
		}
	}

	private void doLogout() {
		Window.Location.assign("/logoutgoogle");
    }

	@Override
	public void go(HasWidgets container) {
	    if ("".equals(History.getToken())) {
	      History.newItem("root");
	    }
	    else {
	      History.fireCurrentHistoryState();
	    }	
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();    
	    if (token != null) {
	       if (token.equals("root")) {
	    	   HeaderPresenter hP = new HeaderPresenter(new HeaderView());
	    	   hP.setUserName(CloudMobile.get().getCurrentUser().getName());
	    	   hP.go(northPanel);
	    	   MenuPresenter wP = new MenuPresenter(eventBus, new MenuView());
	    	   wP.go(westPanel);
	    	   centerPanel.clear();
	    	   centerPanel.add(new WelcomeView());
	    	   return;
	       } else if (token.equals("login")) {
              CloudMobile.get().showLoginView();
              return;
          }
	    }		
	}

}
